package edu.uidaho.RG;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import android.util.DisplayMetrics;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
//import android.view.KeyEvent;
import android.os.Message;


public class GameState extends Object {
	// screen width and height
	
//	DisplayMetrics metrics = new DisplayMetrics();
//	getWindowManager().getDefaultDisplay().getMetrics(metrics);
//	final int _screenWidth = metrics.widthPixels; //300 x 420, Xformer is 1280x800
//	final int _screenHeight = metrics.heightPixels;
	
	final int _screenWidth = 1280; //300 x 420, Xformer is 1280x800
	final int _screenHeight = 600; // some room for top bar
	
	char[] paddleDirection = {'R','L'}; //'L'/'R' -- Start off going right on AI
	int paddleTurn = 1; // use to alternate which paddle comes back and forth
	
	// The bats
	final int _batLength = _screenWidth/10;
	final int _batHeight = _screenHeight/10;
	final int _bigbatLength = _screenWidth/8;
	final int _bigbatHeight = _screenHeight/8;
	
	//refactor into arrays later
	int _userBatX = (_screenWidth / 2) - (_batLength / 2);
	final int _userBatY = _screenHeight/4 + (_bigbatHeight/2 - _batHeight/2);
	int _userBatX2 = (_screenWidth / 2) - (_batLength / 2);
	final int _userBatY2 = _screenHeight*2/4 + (_bigbatHeight/2 - _batHeight/2);
	
	int[] _userControl = {0,0}; // control for the two users
	
	//int _bigBatX = (_screenWidth / 2) - (_batLength / 2); // start in middle

	int[] _bigBatX = {0, 0}; // Start both on left
	final int[] _bigBatY = {_screenHeight/4, _screenHeight*2/4};
	
	// The update method
	public void update() {		 

		_userBatX = messagedata.dataplayer1*( (_screenWidth - _batLength) / 125); //arduino sends a value scaled from 1 to 125
		_userBatX2 = messagedata.dataplayer2*( (_screenWidth - _batLength) / 125);
		_userControl[0] = messagedata.outputPWM1 * _screenWidth / 125;
		_userControl[1] = messagedata.outputPWM2 * _screenWidth / 125;

		lightLED(); // light LED if user is in the zone of big rectangle
		bigPaddleAIsimple(); //updates position of paddle
		setpointOutput();

	}

	/*
		public boolean keyPressed(int keyCode, KeyEvent msg) {
			if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) // left
			{
				_topBatX += _batSpeed;
				_bottomBatX -= _batSpeed;
			}
	
			if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) // right
			{
				_topBatX -= _batSpeed;
				_bottomBatX += _batSpeed;
			}
	
			return true;
		}*/

	// the draw method
	public void draw(Canvas canvas, Paint paint) {

		// Clear the screen
		canvas.drawRGB(20, 20, 20);

		// set the colour
		paint.setARGB(200, 0, 200, 0);
		
		//Draw the two AI bats
		canvas.drawRect(new Rect(_bigBatX[0], _bigBatY[0], _bigBatX[0]
				+ _bigbatLength, _bigBatY[0] + _bigbatHeight), paint);
		
		canvas.drawRect(new Rect(_bigBatX[1], _bigBatY[1], _bigBatX[1]
				+ _bigbatLength, _bigBatY[1] + _bigbatHeight), paint);
		
		// set the colour
		paint.setARGB(200, 200, 200, 0);

		//Draw the two user bats
		canvas.drawRect(new Rect(_userBatX, _userBatY, _userBatX + _batLength,
				_userBatY + _batHeight), paint);
		canvas.drawRect(new Rect(_userBatX2, _userBatY2, _userBatX2 + _batLength,
				_userBatY2 + _batHeight), paint);
		
		//*********
		//Draw the two pwm outputs
		//*********
		paint.setARGB(200, 200, 0, 0);

		//output signal is to right of middle
		if(_userControl[0] > _screenWidth/2){
			canvas.drawRect(new Rect(_screenWidth/2, _bigBatY[0] + _bigbatHeight, _userControl[0],
					_bigBatY[0] + _bigbatHeight*3/2), paint);
		}
		//output 1 is to left of middle
		else{
			canvas.drawRect(new Rect(_userControl[0], _bigBatY[0] + _bigbatHeight, _screenWidth/2,
					_bigBatY[0] + _bigbatHeight*3/2), paint);
		}
		
		if(_userControl[1] > _screenWidth/2){
			canvas.drawRect(new Rect(_screenWidth/2, _bigBatY[1] + _bigbatHeight, _userControl[1],
					_bigBatY[1] + _bigbatHeight*3/2), paint);
		}
		//output 2 is to left of middle
		else{
			canvas.drawRect(new Rect(_userControl[1], _bigBatY[1] + _bigbatHeight, _screenWidth/2,
					_bigBatY[1] + _bigbatHeight*3/2), paint);
		}
	}

	public void pushedData(int readVar){
		//readIn = readVar;
	}
	
	public void bigPaddleAIsimple(){
		/*
		 * Let the big paddle move around back and forth. Try to stay inside of it to turn on the LED.
		 * One paddle moves and one is stationary for this AI
		 */
		final int SPEED = 3;
		 // update both paddles
	    int i = paddleTurn; // 0 or 1 depending on current paddle
		if (paddleDirection[i] == 'R'){
			if (_bigBatX[i] >= _screenWidth - _bigbatLength - SPEED){ //hits the end going right
				paddleDirection[i] = 'L'; // now go left
			}
			_bigBatX[i] += SPEED;
		}

		if (paddleDirection[i] == 'L'){
			if (_bigBatX[i] <= 0 + SPEED){ //hits the end going right
				paddleDirection[i] = 'R'; // now go right
				switchPaddleTurns(); // hits end going right, now let the other one go
			}
			_bigBatX[i] -= SPEED;
		}
	}
	
	public void switchPaddleTurns(){
		if (paddleTurn == 1){
			paddleTurn = 0;
		}
		else if (paddleTurn == 0){
			paddleTurn = 1;
		}
	}
	
	public void lightLED(){
		/*
		 * if user keeps paddle inside bigger box, light LED
		 */
		
		/******
		 *Top Bat
		 ******/
		if ((_userBatX >= _bigBatX[0]) && ((_userBatX + _batLength) <= (_bigBatX[0] + _bigbatLength) )){ // if the little bat is inside of the big bat
			
			messagedata.userInTopBox = 1;
		}
		else{
			messagedata.userInTopBox = 0;
		}
		
		/******
		 *Bottom Bat
		 ******/
		if ((_userBatX2 >= _bigBatX[1]) && ((_userBatX2 + _batLength) <= (_bigBatX[1] + _bigbatLength) )){ // if the little bat is inside of the big bat
			
			messagedata.userInBottomBox = 1;
		}
		else{
			messagedata.userInBottomBox = 0;
		}
	}
	
	public void setpointOutput(){
		/*
		 * This function sends setpoint (the big box center minus the small box center) to the Arduino. The number it sends is scaled from 0 to 10,000 as an int
		 * sends as a float ~~ is 32 bits I think
		 */
		final int _width = _screenWidth - _bigbatLength; // This is the width the bat middle moves between
		final int _sizediff = _bigbatLength/2 - _batLength/2;
		int[] setpoint = {(int)((_bigBatX[0]+_sizediff) * (10000.0 / _width)) , (int)((_bigBatX[1]+_sizediff) * (10000.0 / _width)) }; 
		
		messagedata.topBoxSetpoint = setpoint[0];
		messagedata.bottomBoxSetpoint = setpoint[1];
		
	}
	
	
}
